package helperClassSearch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CreateSitemap {

	public static void createSitemapFile() {
		try {
			File oldSiteMap = new File(
					"C:\\Workspace\\passes\\war\\sitemap.xml");
			oldSiteMap.delete();
			Workbook workbook = Workbook.getWorkbook(new File(
					"C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(9);
			Cell[] count = sheet.getColumn(0);
			Cell[] count1 = sheet.getColumn(1);
			Cell[] count2 = sheet.getColumn(2);
			Cell[] count3 = sheet.getColumn(3);
			FileWriter fstream = new FileWriter(
					"C:\\Workspace\\passes\\war\\sitemap.xml");
			BufferedWriter xmlFile = new BufferedWriter(fstream);
			xmlFile.write("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n<url>\n<loc>http://www.cyclingthealps.com/</loc>\n<priority>0.8</priority>\n</url>\n");
			for (int i = 0; i < count.length; i++) {
				Cell linkCell = sheet.getCell(0, i);
				String link = linkCell.getContents();
				System.out.println(link);
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/#!");
				String extractedLink = link.replaceAll(".*, ", "");
				String extractedLink2 = extractedLink.replaceAll("\"", "");
				String extractedLink3 = extractedLink2.substring(0,
						extractedLink2.length() - 2);
				xmlFile.write(extractedLink3);
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
			}
			for (int j = 0; j<count1.length; j++){
				Cell sitenameCell = sheet.getCell(1,j);
				String siteName = sitenameCell.getContents();
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/game/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/tour/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/tour/" + siteName + "-reverse.html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/streetview/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/streetview/" + siteName + "-reverse.html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/profile/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/explore/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
			}
			for (int k = 0; k<count2.length; k++){
				Cell sitenameCell = sheet.getCell(2,k);
				String siteName = sitenameCell.getContents();
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>http://www.cyclingthealps.com/explore/" + siteName + ".html");
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
			}
			for (int l = 0; l<count3.length; l++){
				Cell sitenameCell = sheet.getCell(3,l);
				String siteName = sitenameCell.getContents();
				xmlFile.write("<url>\n");
				xmlFile.write("<loc>"+ siteName);
				xmlFile.write("</loc>\n<priority>0.8</priority>\n</url>\n");
			}
			xmlFile.write("</urlset>");
			xmlFile.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
